package com.jyr.ohs.service.impl;

import com.jyr.ohs.common.FileUtils;
import com.jyr.ohs.common.UnifiedJsonReturn;
import com.jyr.ohs.entity.*;
import com.jyr.ohs.mapper.*;
import com.jyr.ohs.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @author ZhongJing </p>
 * @Description </p>
 * @date 2021/4/14 5:53 下午 </p>
 */
@Slf4j
@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    StudentMapper studentMapper;

    @Autowired
    CourseMapper courseMapper;

    @Autowired
    IssueMapper issueMapper;

    @Autowired
    HomeworkMapper homeworkMapper;

    @Autowired
    SubmitWorkMapper submitWorkMapper;

    @Override
    public UnifiedJsonReturn<Student> login(String id, String password) {
        // 根据id查找学生
        Student student = studentMapper.selectById(id);

        // 如果查到为null，则表示该id的学生不存在
        if (student == null) {
            return new UnifiedJsonReturn<Student>().failed().message("用户名不存在，请检查用户名是否输入正确");
        }

        // 如果不为null，判断密码是否正确
        if (student.getPassword().equals(password)) {
            // 密码正确成功登陆
            return new UnifiedJsonReturn<Student>().success().message("登陆成功").data(student);
        }

        // 密码错误，登陆失败并返回提示信息
        return new UnifiedJsonReturn<Student>().failed().message("密码错误，请重新输入密码！");

    }

    @Override
    public List<Course> showCourse(String id) {
        return courseMapper.getCourseListBySid(id);
    }

    @Override
    public List<Issue> listIssueByCid(Integer cid) {
        return issueMapper.listByCid(cid);
    }

    @Override
    public boolean submitHomework(SubmitWork submitWork, MultipartFile file) {
        // 判断文件是否为空
        if (file == null) {
            return false;
        }

        try {
            // 获取上传的文件的后缀名
            String filename = file.getOriginalFilename();

            // 设置目录
            String path = new File("/Users/zhangfan").getCanonicalPath();

            // 拼接路径，规则：项目路径/resources/homework/教师id/课程id/作业名称+上传文件的后缀名
            String filePath = path + "/homework/student/" + submitWork.getId() + "/" + submitWork.getSid() + "/" + filename;

            // 文件夹路径
            String dirPath = path + "/homework/student/" + submitWork.getId() + "/" + submitWork.getSid();

            // 写入文件
            String writePath = FileUtils.write(file, filePath, dirPath);

            // 保存作业信息
            submitWork.setScore(0);
            submitWork.setSavePath(writePath);
            submitWorkMapper.add(submitWork);

            // 对应id的作业表提交数量+1
            homeworkMapper.selfIncreasingSubmitCount(submitWork.getId());

        } catch (IOException e) {
            log.warn("写入出错，错误类型：" + e.getClass());
            log.warn("错误信息：" + e.getMessage());
            return false;
        }
        return true;
    }

    @Override
    public Issue getIssueById(Integer id) {
        return issueMapper.getById(id);
    }
}
